package com.woniuxy.joylifemarket.controller;

import com.alibaba.fastjson.JSON;
import com.woniuxy.common.entity.*;
import com.woniuxy.joylifemarket.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class OrderController {
    @Autowired
    MarketOrderService marketOrderService;

    //查询登录用户卖出商品订单
    @RequestMapping("/searchisell")
    public Object iSell(String loginuserid) {
        Map<String, Object> result = new HashMap<>();
        try {
            List<MarketOrder> allOrderBySendUserId = marketOrderService.findAllOrderBySendUserId(loginuserid);
            result.put("code", 200);
            result.put("msg", "查询成功");
            result.put("data",allOrderBySendUserId);
//            System.out.println(allOrderBySendUserId);
            return result;
        } catch (Exception e) {
            result.put("code", 500);
            result.put("msg", "查询失败");
            e.printStackTrace();
            return result;
        }
    }

    //查询登录用户买到商品订单
    @RequestMapping("/searchibuy")
    public Object iBuy(String loginuserid) {
        Map<String, Object> result = new HashMap<>();
        try {
            List<MarketOrder> allOrderByBuyUserId = marketOrderService.findAllOrderByBuyUserId(loginuserid);
            result.put("code", 200);
            result.put("msg", "查询成功");
            result.put("data",allOrderByBuyUserId);
//            System.out.println(allOrderByBuyUserId);
            return result;
        } catch (Exception e) {
            result.put("code", 500);
            result.put("msg", "查询失败");
            e.printStackTrace();
            return result;
        }
    }

    //主键查询订单
    @RequestMapping("/searchorder")
    public Object searchOrder(String orderid) {
        Map<String, Object> result = new HashMap<>();
        try {
            MarketOrder order = marketOrderService.findOrderById(orderid);
            result.put("code", 200);
            result.put("msg", "查询成功");
            result.put("data",order);
            return result;
        } catch (Exception e) {
            result.put("code", 500);
            result.put("msg", "查询失败");
            e.printStackTrace();
            return result;
        }
    }

    //新增订单
    @RequestMapping("/insertorder")
    public Object insertOrder(String order) {
        Map<String, Object> result = new HashMap<>();
        MarketOrder marketOrder = JSON.parseObject(order, MarketOrder.class);
//        System.out.println(marketOrder);
        try {
            MarketOrder newMarketOrder = marketOrderService.insertOrder(marketOrder);
            if (newMarketOrder!=null) {
                result.put("code", 200);
                result.put("msg", "查询成功");
                result.put("data",newMarketOrder);
                return result;
            }
            result.put("code", 500);
            result.put("msg", "查询失败");
            return result;
        } catch (Exception e) {
            result.put("code", 500);
            result.put("msg", "查询失败");
            e.printStackTrace();
            return result;
        }
    }

    //支付订单
    @RequestMapping("/payorder")
    public Object payOrder(String orderId) {
        Map<String, Object> result = new HashMap<>();
        System.out.println(orderId);
        try {
            int i = marketOrderService.updateOrderStatusWithPay(orderId);
            if (i!=0) {
                result.put("code", 200);
                result.put("msg", "查询成功");
                return result;
            }
            result.put("code", 500);
            result.put("msg", "查询失败");
            return result;
        } catch (Exception e) {
            result.put("code", 500);
            result.put("msg", "查询失败");
            e.printStackTrace();
            return result;
        }
    }

    //签收订单
    @RequestMapping("/signorder")
    public Object signOrder(String orderId) {
        System.out.println("orderId"+orderId);
        Map<String, Object> result = new HashMap<>();
        try {
            int i = marketOrderService.updateOrderStatusWithSign(orderId);
            if (i!=0) {
                result.put("code", 200);
                result.put("msg", "查询成功");
                return result;
            }
            result.put("code", 500);
            result.put("msg", "查询失败");
            return result;
        } catch (Exception e) {
            result.put("code", 500);
            result.put("msg", "查询失败");
            e.printStackTrace();
            return result;
        }
    }
}
